<?php

class CacheController extends Controller
{
	public function actionIndex()
	{
		$this->render('index');
	}

	//行业缓存 
	public function actionGetCategory(){
		//清除行业缓存
		if(isset($_GET['flush'])){
			Yii::app()->filecache->delete('category');
			return true;
		}
		//如果存在缓存，调用缓存数据
		if($json = Yii::app()->filecache->get('category')){
			echo $json;
		}else{
			$criteria = new CDbCriteria;
			$criteria->compare('zca_level','1');
			$data = Category::model()->findAll($criteria);
			$item['0'] = $data = CHtml::listData($data,'zca_id','zca_title');
			foreach($data as $key=>$val){
				$criteria = new CDbCriteria;
				$criteria->compare('zca_parentid',$key);
				$data2 = Category::model()->findAll($criteria);
				if(!empty($data2))
					$item['0,'.$key] = $data2 = CHtml::listData($data2,'zca_id','zca_title');
				foreach($data2 as $key2=>$val2){
					$criteria = new CDbCriteria;
					$criteria->compare('zca_parentid',$key2);
					$data3 = Category::model()->findAll($criteria);
					if(!empty($data3))
						$item['0,'.$key.",".$key2] = $data3 = CHtml::listData($data3,'zca_id','zca_title');
				}
			}
			$json = json_encode($item);
			//设置缓存
			Yii::app()->filecache->set('category',$json,3600*24);
			echo $json;
		}
	}
	
	//地区缓存
	public function actionGetArea(){
		//清除行业缓存
		//if(isset($_GET['flush'])){
			Yii::app()->filecache->delete('area');
		//	return true;
		//}
		//如果存在缓存，调用缓存数据
		if($json = Yii::app()->filecache->get('area')){
			echo $json;
		}else{
			$criteria = new CDbCriteria;
			$criteria->compare('zar_parentid','0');
			$data = Area::model()->findAll($criteria);
			foreach($data as $key=>$val){
				$item[$key]['zar_id'] = $val->zar_id;
				$item[$key]['zar_title'] = $val->zar_title;
				$criteria = new CDbCriteria;
				$criteria->compare('zar_parentid',$val->zar_id);
				$data2 = Area::model()->findAll($criteria);
				foreach($data2 as $key2=>$val2){
					$item[$key]['child'][$key2]['zar_id'] = $val2->zar_id;
					$item[$key]['child'][$key2]['zar_title'] = $val2->zar_title;
					$criteria = new CDbCriteria;
					$criteria->compare('zar_parentid',$val2->zar_id);
					$data3 = Area::model()->findAll($criteria);
					foreach($data3 as $key3=>$val3){
						$item[$key]['child'][$key2]['child'][$key3]['zar_id'] = $val3->zar_id;
						$item[$key]['child'][$key2]['child'][$key3]['zar_title'] = $val3->zar_title;
					}
				}
			}
			$json = json_encode($item);
			//设置缓存
			Yii::app()->filecache->set('area',$json,3600*24);
			echo $json;
		}
	}
	
	//信息分类缓存
	public function actionGetNewsCat(){
		//清除行业缓存
		if(isset($_GET['flush'])){
			Yii::app()->filecache->delete('newscat');
			return true;
		}
		//如果存在缓存，调用缓存数据
		if($json = Yii::app()->filecache->get('newscat')){
			echo $json;
		}else{
			$criteria = new CDbCriteria;
			$criteria->compare('znc_patentid','0');
			$data = NewsCat::model()->findAll($criteria);
			$item['0'] = $data = CHtml::listData($data,'znc_id','znc_title');
			foreach($data as $key=>$val){
				$criteria = new CDbCriteria;
				$criteria->compare('znc_patentid',$key);
				$data2 = NewsCat::model()->findAll($criteria);
				if(!empty($data2))
					$item['0,'.$key] = $data2 = CHtml::listData($data2,'znc_id','znc_title');
				foreach($data2 as $key2=>$val2){
					$criteria = new CDbCriteria;
					$criteria->compare('znc_patentid',$key2);
					$data3 = NewsCat::model()->findAll($criteria);
					if(!empty($data3))
						$item['0,'.$key.",".$key2] = $data3 = CHtml::listData($data3,'znc_id','znc_title');
				}
			}
			$json = json_encode($item);
			//设置缓存
			Yii::app()->filecache->set('newscat',$json,3600*24);
			echo $json;
		}
	}
	
	/**
	 * 帮助分类缓存
	 */
	public function actionGethelpCat(){
		echo  HelpCat::model()->getHelpCategoryJson();
	}
	
}